package com.homedo.odin.solution.util;

import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.SolutionAddReq;
import com.homedo.odin.solution.contract.pc.SSolutionDetailResp;
import com.homedo.odin.solution.contract.pc.SolutionAddPcReq;
import com.homedo.odin.solution.contract.pc.SolutionEditPcReq;
import com.homedo.odin.solution.enumcode.basic.ChargeEnum;
import com.homedo.odin.solution.enumcode.basic.DownloadEnum;
import com.homedo.odin.solution.enumcode.other.HandleTypeEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionExpTypeEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.model.dto.SolutionResponseBean;
import com.homedo.odin.solution.model.entity.Solution;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class SolutionBeansConvertUtil {

    public static Solution convert(SolutionAddReq solutionAddBean) {
        if (solutionAddBean == null) {
            return null;
        }
        Solution solution = new Solution();
        solution.setAttachment(solutionAddBean.getAttachment());
        solution.setBuildingType(solutionAddBean.getBuildingType());
        solution.setDescription(solutionAddBean.getDescription());
        if (solutionAddBean.getSolutionType().intValue() == SolutionTypeEnum.EXPERT_SOLUTION.getDbValue()) {
            solution.setExpertId(solutionAddBean.getExpertId());
            solution.setExpertName(solutionAddBean.getExpertName());
            solution.setUserId(solutionAddBean.getExpertId());
            solution.setUserName(solutionAddBean.getExpertName());
        }
        solution.setIsAdmitDownload(solutionAddBean.getIsAdmitDownload());
        solution.setMatchPic(solutionAddBean.getMatchPic());
        solution.setCasePic(solutionAddBean.getCasePic());
        solution.setSolutionKeyword(solutionAddBean.getSolutionKeyword());
        solution.setSolutionName(solutionAddBean.getSolutionName());
        solution.setSolutionType(solutionAddBean.getSolutionType());
        solution.setDownloadCount(solutionAddBean.getDownloadCount());
        solution.setPageView(solutionAddBean.getPageView());
        solution.setFirstSysAttr(solutionAddBean.getFirstSysAttr());
        solution.setSecondSysAttr(solutionAddBean.getSecondSysAttr());
        solution.setIsRecommend(solutionAddBean.getIsRecommend());
        solution.setSort(solutionAddBean.getSort());
        solution.setBusinessCode(solutionAddBean.getBusinessCode());
        if (DownloadEnum.CAN_DOWNLOAD.getDbValue().equals(solutionAddBean.getIsAdmitDownload())) {
            solution.setIsFree(solutionAddBean.getIsFree());
            if (ChargeEnum.CHARGE.getDbValue().equals(solutionAddBean.getIsFree())) {
                solution.setExpType(solutionAddBean.getExpType());
                solution.setOriginalPrice(solutionAddBean.getCharges());
                solution.setCharges(solutionAddBean.getCharges());
            } else {
                solution.setExpType(SolutionExpTypeEnum.FREE.getDbValue());
                solution.setOriginalPrice(BigDecimal.ZERO);
                solution.setCharges(BigDecimal.ZERO);
            }
        } else {
            solution.setIsFree(0);
            solution.setExpType(0);
            solution.setOriginalPrice(BigDecimal.ZERO);
            solution.setCharges(BigDecimal.ZERO);
        }
        return solution;
    }

    public static void convert(SolutionAddReq solutionEditBean,
            Solution solution) {
        if (solutionEditBean == null || solution == null) {
            return;
        }
        //必填字段
        if (StringUtils.isNotEmpty(solutionEditBean.getSolutionName())) {
            solution.setSolutionName(solutionEditBean.getSolutionName());
        }
        if (NumberHelper.validate(solutionEditBean.getDownloadCount())) {
            solution.setDownloadCount(solutionEditBean.getDownloadCount());
        }
        if (NumberHelper.validate(solutionEditBean.getPageView())) {
            solution.setPageView(solutionEditBean.getPageView());
        }
        if (HandleTypeEnum.dbValueExist(solutionEditBean.getIsAdmitDownload())) {
            solution.setIsAdmitDownload(solutionEditBean.getIsAdmitDownload());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getBuildingType())) {
            solution.setBuildingType(solutionEditBean.getBuildingType());
        }
        if (NumberHelper.validatePositive(solutionEditBean.getFirstSysAttr())) {
            solution.setFirstSysAttr(solutionEditBean.getFirstSysAttr());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getSecondSysAttr())) {
            solution.setSecondSysAttr(solutionEditBean.getSecondSysAttr());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getAttachment())) {
            solution.setAttachment(solutionEditBean.getAttachment());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getMatchPic())) {
            solution.setMatchPic(solutionEditBean.getMatchPic());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getCasePic())) {
            solution.setCasePic(solutionEditBean.getCasePic());
        }
        //非必填字段
        if (solutionEditBean.getSolutionType().intValue() == SolutionTypeEnum.EXPERT_SOLUTION.getDbValue()) {
            solution.setExpertId(solutionEditBean.getExpertId());
            solution.setExpertName(solutionEditBean.getExpertName());
            solution.setUserId(solutionEditBean.getExpertId());
            solution.setUserName(solutionEditBean.getExpertName());
        } else {
            solution.setExpertId(0);
            solution.setExpertName(null);
        }
        solution.setBusinessCode(solutionEditBean.getBusinessCode());
        solution.setBrandId(solutionEditBean.getBrandId());
        solution.setDescription(solutionEditBean.getDescription());
        solution.setSolutionKeyword(solutionEditBean.getSolutionKeyword());
        solution.setSort(solutionEditBean.getSort());
        if (HandleTypeEnum.dbValueExist(solutionEditBean.getIsRecommend())) {
            solution.setIsRecommend(solutionEditBean.getIsRecommend());
        }

        if (DownloadEnum.CAN_DOWNLOAD.getDbValue().equals(solutionEditBean.getIsAdmitDownload())) {
            solution.setIsFree(solutionEditBean.getIsFree());
            if (ChargeEnum.CHARGE.getDbValue().equals(solutionEditBean.getIsFree())) {
                solution.setExpType(solutionEditBean.getExpType());
                solution.setOriginalPrice(solutionEditBean.getCharges());
                solution.setCharges(solutionEditBean.getCharges());
            } else {
                solution.setExpType(SolutionExpTypeEnum.FREE.getDbValue());
                solution.setOriginalPrice(BigDecimal.ZERO);
                solution.setCharges(BigDecimal.ZERO);
            }
        } else {
            solution.setIsFree(0);
            solution.setExpType(0);
            solution.setOriginalPrice(BigDecimal.ZERO);
            solution.setCharges(BigDecimal.ZERO);
        }
    }


    public static void convert(Solution solution, SolutionResponseBean solutionResponse) {
        if (solution == null || solutionResponse == null) {
            return;
        }
        solutionResponse.setCollectCount(solution.getCollectCount());
        solutionResponse.setCreateTime(solution.getCreateTime());
        solutionResponse.setDescription(solution.getDescription());
        solutionResponse.setDownloadCount(solution.getDownloadCount());
        solutionResponse.setIsRecommend(solution.getIsRecommend());
        solutionResponse.setMatchPic(solution.getMatchPic());
        solutionResponse.setPageView(solution.getPageView());
        solutionResponse.setSolutionId(solution.getSolutionId());
        solutionResponse.setSolutionName(solution.getSolutionName());
        solutionResponse.setSolutionType(solution.getSolutionType());
        solutionResponse.setStatus(solution.getStatus());
        solutionResponse.setStatusStr(SolutionStateEnum.getExistDisplayValue(solution.getStatus()));
        solutionResponse.setIsNew(isLatestWeek(solution.getCreateTime()));
        solutionResponse.setIsAdmitDownload(solution.getIsAdmitDownload());
        solutionResponse.setExpType(solution.getExpType());
        solutionResponse.setUserId(solution.getUserId());
        solutionResponse.setCommentCount(solution.getCommentCount());
        solutionResponse.setCharges(solution.getCharges());
        solutionResponse.setIsHot(false);
    }

    private static Boolean isLatestWeek(Date addtime) {
        Date now = new Date();
        //得到日历
        Calendar calendar = Calendar.getInstance();
        //把当前时间赋给日历
        calendar.setTime(now);
        //设置为7天前
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        //得到7天前的时间
        Date before7days = calendar.getTime();
        if (before7days.getTime() < addtime.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    public static SSolutionDetailResp convert(Solution solution) {
        if (solution == null) {
            return null;
        }
        SSolutionDetailResp sSolutionDetail = new SSolutionDetailResp();
        sSolutionDetail.setOriginalPrice(solution.getOriginalPrice());
        sSolutionDetail.setCharges(solution.getCharges());
        sSolutionDetail.setCreateTime(solution.getCreateTime());
        sSolutionDetail.setDescription(solution.getDescription());
        sSolutionDetail.setDownloadCount(solution.getDownloadCount());
        sSolutionDetail.setSolutionId(solution.getSolutionId());
        sSolutionDetail.setSolutionKeyword(solution.getSolutionKeyword());
        sSolutionDetail.setSolutionName(solution.getSolutionName());
        sSolutionDetail.setSolutionType(solution.getSolutionType());
        sSolutionDetail.setIsAdmitDownload(solution.getIsAdmitDownload());
        sSolutionDetail.setIsCollect(0);
        sSolutionDetail.setIsMy(0);
        sSolutionDetail.setMatchPic(solution.getMatchPic());
        sSolutionDetail.setPageView(solution.getPageView() + 1);
        sSolutionDetail.setLikeCount(solution.getLikeCount());
        sSolutionDetail.setCommentCount(solution.getCommentCount());
        sSolutionDetail.setAttachment(solution.getAttachment());
        sSolutionDetail.setCasePic(solution.getCasePic());
        sSolutionDetail.setExpType(solution.getExpType());
        sSolutionDetail.setIsNew(isLatestWeek(solution.getCreateTime()));
        sSolutionDetail.setStatus(solution.getStatus());
        sSolutionDetail.setIsRecommend(solution.getIsRecommend());
        sSolutionDetail.setOrderNo(new Long(0));
        sSolutionDetail.setPayStatus(0);
        sSolutionDetail.setIsFree(solution.getIsFree());
        sSolutionDetail.setUserId(solution.getUserId());
        sSolutionDetail.setActivitySolution(validateSolutionType(solution.getSolutionId()));
        sSolutionDetail.setAuditOpinion(solution.getAuditOpinion());
        sSolutionDetail.setBuildingType(solution.getBuildingType());
        sSolutionDetail.setSecondSysAttr(solution.getSecondSysAttr());
        sSolutionDetail.setFirstSysAttr(solution.getFirstSysAttr());
        return sSolutionDetail;
    }

    public static boolean validateSolutionType(Long solutionType) {
        return ArrayUtils.contains(
                new Long[]{new Long("74783920267776000"), new Long("74504974506704896"), new Long("107809179266760704"),
                        new Long("74455924688343040"),
                        new Long("74793547965452288"), new Long("74455015505838080"), new Long("74826279470809088"),
                        new Long("107807429252136960"),
                        new Long("74797352601829376"), new Long("74521520062910464"), new Long("74794983017205760"),
                        new Long("74550972994080768"),
                        new Long("74463097396969472"), new Long("74519920846094336"), new Long("88699401722707968"),
                        new Long("74507364521791488"),
                        new Long("74455346860052480"), new Long("74536669410803712"), new Long("74791444245168128"),
                        new Long("74468261054955520"),}, solutionType);
    }

    public static boolean validate(SolutionAddPcReq solutionAddBean) {
        if (solutionAddBean == null) {
            return false;
        }
        if (!NumberHelper.validatePositive(solutionAddBean.getUserId())
                || StringUtils.isBlank(solutionAddBean.getSolutionName())
                || !NumberHelper.validatePositive(solutionAddBean.getFirstSysAttr())
                || StringUtils.isBlank(solutionAddBean.getSecondSysAttr())
                || !DownloadEnum.dbValueExist(solutionAddBean.getIsAdmitDownload())
                || StringUtils.isBlank(solutionAddBean.getAttachment())
                || StringUtils.isBlank(solutionAddBean.getMatchPic())) {
            return false;
        }
        if (DownloadEnum.CAN_DOWNLOAD.getDbValue().equals(solutionAddBean.getIsAdmitDownload())) {
            if (!NumberHelper.validate(solutionAddBean.getIsFree()) ||
                    !NumberHelper.validate(solutionAddBean.getExpType()) ||
                    !NumberHelper.validate(solutionAddBean.getCharges())) {
                return false;
            }
        }
        return true;
    }

    public static void convert(Solution solution, SolutionAddPcReq solutionAddBean) {
        if (solution == null || solutionAddBean == null) {
            return;
        }
        solution.setUserId(solutionAddBean.getUserId());
        solution.setUserName(solutionAddBean.getUserName());
        solution.setAttachment(solutionAddBean.getAttachment());
        solution.setBuildingType(solutionAddBean.getBuildingType());
        solution.setDescription(solutionAddBean.getDescription());
        solution.setIsAdmitDownload(solutionAddBean.getIsAdmitDownload());
        solution.setMatchPic(solutionAddBean.getMatchPic());
        solution.setSolutionKeyword(solutionAddBean.getSolutionKeyword());
        solution.setSolutionName(solutionAddBean.getSolutionName());
        solution.setFirstSysAttr(solutionAddBean.getFirstSysAttr());
        solution.setSecondSysAttr(solutionAddBean.getSecondSysAttr());
        solution.setCasePic(solutionAddBean.getCasePic());
        if (DownloadEnum.CAN_DOWNLOAD.getDbValue().equals(solutionAddBean.getIsAdmitDownload())) {
            solution.setIsFree(solutionAddBean.getIsFree());
            if (ChargeEnum.CHARGE.getDbValue().equals(solutionAddBean.getIsFree())) {
                solution.setExpType(solutionAddBean.getExpType());
                solution.setOriginalPrice(solutionAddBean.getCharges());
                solution.setCharges(solutionAddBean.getCharges());
            } else {
                solution.setExpType(SolutionExpTypeEnum.FREE.getDbValue());
                solution.setOriginalPrice(BigDecimal.ZERO);
                solution.setCharges(BigDecimal.ZERO);
            }
        }
    }

    public static void convert(SolutionEditPcReq solutionEditBean,
            Solution solution) {
        if (solutionEditBean == null || solution == null) {
            return;
        }
        //必填字段
        if (StringUtils.isNotEmpty(solutionEditBean.getSolutionName())) {
            solution.setSolutionName(solutionEditBean.getSolutionName());
        }
        if (HandleTypeEnum.dbValueExist(solutionEditBean.getIsAdmitDownload())) {
            solution.setIsAdmitDownload(solutionEditBean.getIsAdmitDownload());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getBuildingType())) {
            solution.setBuildingType(solutionEditBean.getBuildingType());
        }
        if (NumberHelper.validatePositive(solutionEditBean.getFirstSysAttr())) {
            solution.setFirstSysAttr(solutionEditBean.getFirstSysAttr());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getSecondSysAttr())) {
            solution.setSecondSysAttr(solutionEditBean.getSecondSysAttr());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getAttachment())) {
            solution.setAttachment(solutionEditBean.getAttachment());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getMatchPic())) {
            solution.setMatchPic(solutionEditBean.getMatchPic());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getBuildingType())) {
            solution.setBuildingType(solutionEditBean.getBuildingType());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getCasePic())) {
            solution.setCasePic(solutionEditBean.getCasePic());
        }
        if (StringUtils.isNoneEmpty(solutionEditBean.getDescription())) {
            solution.setDescription(solutionEditBean.getDescription());
        }
        if (StringUtils.isNotEmpty(solutionEditBean.getSolutionKeyword())) {
            solution.setSolutionKeyword(solutionEditBean.getSolutionKeyword());
        }
        if (solutionEditBean.getIsAdmitDownload().intValue() == DownloadEnum.CAN_DOWNLOAD.getDbValue()) {
            solution.setIsFree(solutionEditBean.getIsFree());
            if (solutionEditBean.getIsFree().intValue() == ChargeEnum.CHARGE.getDbValue()) {
                solution.setExpType(solutionEditBean.getExpType());
                solution.setOriginalPrice(solutionEditBean.getCharges());
                solution.setCharges(solutionEditBean.getCharges());
            } else {
                solution.setExpType(SolutionExpTypeEnum.FREE.getDbValue());
                solution.setOriginalPrice(BigDecimal.ZERO);
                solution.setCharges(BigDecimal.ZERO);
            }
        } else {
            solution.setExpType(SolutionExpTypeEnum.FREE.getDbValue());
            solution.setOriginalPrice(BigDecimal.ZERO);
            solution.setCharges(BigDecimal.ZERO);
        }
    }
}
